package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.entity.Dir;
import com.boot.entity.FileInfo;
import com.boot.mapper.DirMapper;
import com.boot.service.IDirService;
import com.boot.service.IFileInfoService;
import com.boot.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bbyh
 * @since 2024-04-27
 */
@Service
public class DirServiceImpl extends ServiceImpl<DirMapper, Dir> implements IDirService {
    @Resource
    private IUserService userService;
    @Resource
    private IFileInfoService fileInfoService;

    @Override
    public void initRootDir(String username) {
        Integer userId = userService.getUserIdByUsername(username);
        Dir dir = new Dir();
        dir.setDirId(0);
        dir.setParentDirId(-1);
        dir.setUserId(userId);
        dir.setDirName("我的网盘");
        dir.setCreateTime(LocalDateTime.now());
        save(dir);
    }

    @Override
    public Dir getDirByDirIdAndUserId(Integer dirId, Integer userId) {
        LambdaQueryWrapper<Dir> dirLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dirLambdaQueryWrapper.eq(Dir::getDirId, dirId).eq(Dir::getUserId, userId);
        return getOne(dirLambdaQueryWrapper);
    }

    @Override
    public Dir getDirByDirIdAndDirName(Integer parentDirId, String dirName, Integer userId) {
        LambdaQueryWrapper<Dir> dirLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dirLambdaQueryWrapper.eq(Dir::getParentDirId, parentDirId).eq(Dir::getUserId, userId).eq(Dir::getDirName, dirName);
        return getOne(dirLambdaQueryWrapper);
    }

    @Override
    public List<Dir> getPathOfDir(Integer dirId, Integer userId) {
        List<Dir> dirPath = new ArrayList<>();
        LambdaQueryWrapper<Dir> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dir::getDirId, dirId).eq(Dir::getUserId, userId);
        Dir dir = getOne(wrapper);
        while (dir != null) {
            dirPath.add(dir);
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dir::getDirId, dir.getParentDirId()).eq(Dir::getUserId, userId);
            dir = getOne(wrapper);
        }
        return dirPath;
    }

    @Override
    public void removeDirRecursiveById(Integer dirId) {
        LambdaQueryWrapper<Dir> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dir::getDirId, dirId);
        remove(wrapper);

        LambdaQueryWrapper<FileInfo> childFileWrapper = new LambdaQueryWrapper<>();
        childFileWrapper.eq(FileInfo::getDirId, dirId);
        List<FileInfo> fileInfoList = fileInfoService.list(childFileWrapper);
        if (fileInfoList.size() > 0) {
            fileInfoService.remove(childFileWrapper);
        }

        LambdaQueryWrapper<Dir> childDirWrapper = new LambdaQueryWrapper<>();
        childDirWrapper.eq(Dir::getParentDirId, dirId);
        List<Dir> childDirList = list(childDirWrapper);
        if (childDirList.size() > 0) {
            for (Dir childDir : childDirList) {
                removeDirRecursiveById(childDir.getDirId());
            }
        }
    }
}
